package com.fr.report.core.cal;

import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.base.core.FT;
import com.fr.data.TableData;
import com.fr.report.AbstractReport;
import com.fr.report.CellElement;
import com.fr.report.Report;
import com.fr.report.ReportPageAttr;
import com.fr.report.ReportSettings;
import com.fr.report.TemplateReport;
import com.fr.report.TemplateWorkBook;
import com.fr.report.cellElement.CellExpandAttr;
import com.fr.report.cellElement.CellGUIAttr;
import com.fr.report.cellElement.TableDataColumn;
import com.fr.report.cellElement.core.DSColumn;
import com.fr.report.cellElement.core.FunctionGrouper;
import com.fr.report.core.DeathCycleException;
import com.fr.report.core.ReportUtils;
import com.fr.report.script.Calculator;
import com.fr.report.script.ParameterMapNameSpace;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LayerSheetExecuter extends SheetExecuter
{
  protected TableData data = null;
  protected DSColumn dsColumn = null;
  protected TemplateReport report;
  protected int report_shrink_mode = 3;
  protected LayerFamilyMember[][] genealogy;
  protected FT repeat_page_head_rows = null;
  protected FT repeat_page_foot_rows = null;

  protected LayerSheetExecuter(TemplateReport paramTemplateReport, Map paramMap)
  {
    this.report = paramTemplateReport;
    this.parameterMap = paramMap;
    ReportPageAttr localReportPageAttr = paramTemplateReport.getReportPageAttr();
    if (localReportPageAttr != null)
    {
      int i = localReportPageAttr.getRepeatHeaderRowFrom();
      int j = localReportPageAttr.getRepeatHeaderRowTo();
      if ((j >= i) && (i >= 0))
        this.repeat_page_head_rows = new FT(i, j + 1);
      int k = localReportPageAttr.getRepeatFooterRowFrom();
      int l = localReportPageAttr.getRepeatFooterRowTo();
      if ((l >= l) && (k >= 0))
        this.repeat_page_foot_rows = new FT(k, l + 1);
    }
    this.calculator = Calculator.createStaticCalculator();
    this.calculator.setCurrentReport(paramTemplateReport);
    this.calculator.setCurrentTableDataSource(paramTemplateReport.getTemplateWorkBook());
    this.report_shrink_mode = ReportUtils.getReportSettings(paramTemplateReport).getShrinkToFitMode();
    Iterator localIterator = this.report.getTemplateWorkBook().getTableDataNameIterator();
    while (localIterator.hasNext())
    {
      TableData localTableData = this.report.getTemplateWorkBook().getTableData((String)localIterator.next());
      __setParameterValues(localTableData.getParameters());
    }
    try
    {
      this.calculator.set("$$totalPage_number", "$$totalPage_number");
      this.calculator.set("$$page_number", "$$page_number");
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    this.calculator.pushNameSpace(ParameterMapNameSpace.create(paramMap));
  }

  protected void buildGenealogy()
  {
    this.genealogy = new LayerFamilyMember[this.report.getRowCount()][this.report.getColumnCount()];
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = this.report.cellIterator();
    while (localIterator.hasNext())
    {
      CellElement localCellElement = (CellElement)localIterator.next();
      buildFamilyMember(localCellElement, localArrayList);
    }
  }

  protected LayerFamilyMember buildFamilyMember(CellElement paramCellElement, List paramList)
  {
    ColumnRow localColumnRow;
    if ((paramCellElement == null) || (paramList == null))
      return null;
    if (this.genealogy[paramCellElement.getRow()][paramCellElement.getColumn()] != null)
      return this.genealogy[paramCellElement.getRow()][paramCellElement.getColumn()];
    if (paramList.contains(paramCellElement))
    {
      paramList.add(paramCellElement);
      throw new DeathCycleException("Death cycle exists when calculate parent: " + paramList);
    }
    paramList.add(paramCellElement);
    CellExpandAttr localCellExpandAttr = paramCellElement.getCellExpandAttr();
    LayerFamilyMember localLayerFamilyMember1 = null;
    LayerFamilyMember localLayerFamilyMember2 = null;
    if ((localColumnRow = localCellExpandAttr.getLeftParentColumnRow()) != null)
    {
      localObject1 = this.report.getCellElement(localColumnRow.column, localColumnRow.row);
      localLayerFamilyMember1 = buildFamilyMember((CellElement)localObject1, paramList);
    }
    if ((localColumnRow = localCellExpandAttr.getUpParentColumnRow()) != null)
    {
      localObject1 = this.report.getCellElement(localColumnRow.column, localColumnRow.row);
      localLayerFamilyMember2 = buildFamilyMember((CellElement)localObject1, paramList);
    }
    Object localObject1 = null;
    boolean bool = false;
    Object localObject2 = paramCellElement.getValue();
    if (localObject2 instanceof DSColumn)
    {
      localObject1 = ((DSColumn)localObject2).getColumn();
      localObject3 = ((DSColumn)localObject2).getGrouper();
      if (localObject3 instanceof FunctionGrouper)
        bool = ((FunctionGrouper)localObject3).getDivideMode() != 1;
    }
    Object localObject3 = new LayerFamilyMember(paramCellElement, localLayerFamilyMember1, localLayerFamilyMember2, (TableDataColumn)localObject1, bool);
    this.genealogy[paramCellElement.getRow()][paramCellElement.getColumn()] = localObject3;
    paramList.remove(paramCellElement);
    if (localLayerFamilyMember1 != null)
    {
      if (localLayerFamilyMember1.sons == null)
        localLayerFamilyMember1.sons = new ArrayList();
      localLayerFamilyMember1.sons.add(localObject3);
    }
    if (localLayerFamilyMember2 != null)
    {
      if (localLayerFamilyMember2.sons == null)
        localLayerFamilyMember2.sons = new ArrayList();
      localLayerFamilyMember2.sons.add(localObject3);
    }
    return ((LayerFamilyMember)(LayerFamilyMember)localObject3);
  }

  protected void dealwith_row_height(CellElement paramCellElement, Report paramReport)
  {
    if ((paramCellElement == null) || (paramReport == null))
      return;
    CellGUIAttr localCellGUIAttr = paramCellElement.getCellGUIAttr();
    if (localCellGUIAttr == null)
      localCellGUIAttr = CellGUIAttr.DEFAULT_CELLGUIATTR;
    if (((this.report_shrink_mode == 1) && (localCellGUIAttr.getAdjustMode() == 3)) || (localCellGUIAttr.getAdjustMode() == 1))
      ((AbstractReport)paramReport).shrinkTOFitRowHeightForCellElement(paramCellElement);
  }

  protected void sort_FamilyMemberArray(LayerFamilyMember[] paramArrayOfLayerFamilyMember)
  {
    if (paramArrayOfLayerFamilyMember == null)
      return;
    Arrays.sort(paramArrayOfLayerFamilyMember, new Comparator(this)
    {
      private final LayerSheetExecuter this$0;

      public int compare(, Object paramObject2)
      {
        return (((LayerSheetExecuter.LayerFamilyMember)paramObject1).rect.top - ((LayerSheetExecuter.LayerFamilyMember)paramObject2).rect.top);
      }
    });
  }

  protected void dealwith_layer(List paramList)
  {
    if (paramList == null)
      return;
    for (int i = 0; i < this.genealogy.length; ++i)
    {
      LayerFamilyMember[] arrayOfLayerFamilyMember = this.genealogy[i];
      int k = 0;
      int l = (arrayOfLayerFamilyMember == null) ? 0 : arrayOfLayerFamilyMember.length;
      while (k < l)
      {
        LayerFamilyMember localLayerFamilyMember = arrayOfLayerFamilyMember[k];
        if ((localLayerFamilyMember != null) && (localLayerFamilyMember.leftParent == null) && (localLayerFamilyMember.upParent == null))
        {
          paramList.add(localLayerFamilyMember);
          if (this.data == null)
          {
            CellElement localCellElement = localLayerFamilyMember.current;
            Object localObject = localCellElement.getValue();
            if (localObject instanceof DSColumn)
            {
              this.dsColumn = ((DSColumn)localObject);
              this.data = __us_name2TableData(this.dsColumn.getDSName());
            }
          }
        }
        ++k;
      }
    }
    i = 0;
    int j = paramList.size();
    while (i < j)
    {
      ((LayerFamilyMember)paramList.get(i)).refresh_rect();
      ++i;
    }
  }

  protected static class LayerFamilyMember extends FamilyMember
  {
    protected TableDataColumn column;
    protected int columnIndex = -2147483648;
    protected Rect rect;
    protected boolean isGroup = false;
    protected ColumnRow current_cr;

    protected LayerFamilyMember(CellElement paramCellElement, LayerFamilyMember paramLayerFamilyMember1, LayerFamilyMember paramLayerFamilyMember2, TableDataColumn paramTableDataColumn, boolean paramBoolean)
    {
      super(paramCellElement, paramLayerFamilyMember1, paramLayerFamilyMember2);
      this.column = paramTableDataColumn;
      this.isGroup = paramBoolean;
      this.current_cr = ColumnRow.valueOf(paramCellElement.getColumn(), paramCellElement.getRow());
      this.rect = new Rect(paramCellElement.getColumn(), paramCellElement.getRow(), paramCellElement.getColumn() + paramCellElement.getColumnSpan() - 1, paramCellElement.getRow() + paramCellElement.getRowSpan() - 1);
    }

    public int getColumnIndex(TableData paramTableData)
    {
      if ((paramTableData == null) || (this.column == null))
        return -2147483648;
      if (this.columnIndex >= -1)
        return this.columnIndex;
      this.columnIndex = this.column.getColumnIndex(paramTableData, null);
      return this.columnIndex;
    }

    public Rect refresh_rect()
    {
      if (this.sons != null)
      {
        int i = 0;
        int j = this.sons.size();
        while (i < j)
        {
          this.rect.union(((LayerFamilyMember)this.sons.get(i)).refresh_rect());
          ++i;
        }
      }
      return this.rect;
    }
  }
}